/**
 * Description:该类是所有控件，容器的基类， 主要用于装载控件和容器的属性和事件
 * Copyright: 成都和为科技有限公司 2012
 * Author:zhouxingxing
 * CreateDate:2012-01-01
 */
function Control() {}

var oControl = new Object();
//已经加载CSS文件, 避免重复加载
Control._loadCSS = {};

/**
 * 初始化方法
 */
Control.prototype.init = function () {
	this.create(); //调用控件的创建HTML方法
	this.build();
}

/**
 * 创建html对象
 */
Control.prototype.create = function(){
	if(!this._html){
		this._html = document.createElement("div");
		$(this._html).attr("id",this._attr.tag);
	}
}

/**
 * 为dom节点封装属性、样式、事件
 * @param {Object} g_sPath
 * @param {Object} obj
 */
Control.prototype.build = function(){
	for(var nodeName in this._attr){
		var nodeValue = this._attr[nodeName];
		
		//封装事件
		if(this.isScriptValue(nodeValue)
			&& $.support.leadingWhitespace){
			this.buildEvent(nodeName,this.getScriptValue(nodeValue));
			continue;
		}
		if (typeof(nodeValue) == "function") {
			this.buildEvent(nodeName, nodeValue);
		}
		
		//封装属性和样式
		switch(nodeName) {
			case "class":
				$(this.getHTML()).addClass(nodeValue);
				break;
            case "datasource":
                this.bulidDataSource(nodeValue);
                break;
			default:
                $(this.getHTML()).attr(nodeName, nodeValue);
				break;
		}
	}
}

/**
 * 判断是否为一个脚本值
 */
Control.prototype.isScriptValue = function(value) {
    if(typeof(value) == "string" && value.substring(0,1) == "{"
        && value.substring(value.length - 1, value.length) == "}")
        return true;
    else
        return false;
}

/**
 * 获得脚本值内容
 */
Control.prototype.getScriptValue = function(value) {
    if(value){
        return value.substring(1,value.length-1);
    }
    return null;
}

Control.prototype.setElement = function(oElement) {
	this._element = oElement;
}

Control.prototype.getElement = function() {
	return this._element;
}

/**
 * 为对象封装事件
 * @param {Object} name  事件名
 * @param {Object} value  处理函数
 * @param {Object} obj
 */
Control.prototype.buildEvent = function (name, value) {
	var othis = this;
	$(this.getHTML()).bind(name, function(e) {
		if (typeof(value) == "function") {
			value.call(this, this, e);
		} else {
			if (!othis.isDisable()) {
				othis.bindEvent(value, e);
			}
		}
	});
}

/**
 * 移除事件
 * @param {} name
 */
Control.prototype.removeEvent = function (name) {
	var othis = this;
	$(this.getHTML()).unbind(name);
}

/**
 * 重绑定方法
 * @param {Object} value
 */
Control.prototype.rebuildEvent = function(){
	for(var nodeName in this._attr){
		var nodeValue = this._attr[nodeName];
		//封装事件
		if(this.isScriptValue(nodeValue)){
			this.removeEvent(nodeName);
			this.buildEvent(nodeName,this.getScriptValue(nodeValue));
			continue;
		}
	}
}

/**
 * 增加事件
 * @param {Object} value  调用方法值
 */
Control.prototype.bindEvent = function(value,a,b,c,d,e){
	var mEvent = value.split(",");
	var value = new Array();

	for(var i = 0 ;i< mEvent.length;i++) {
		var eventMethod = mEvent[i];
		if(eventMethod.indexOf(".") != -1) {
			value[i] =  eval(eventMethod + "(this,a,b,c,d,e)");
		} else {
			value[i] =  eval("this.getComponent().getHandleClass()." + eventMethod + "(this,a,b,c,d,e)");
		}
	}
	if (value.length == 1) {
		return value[0];
	} else {
		return value;
	}
}

/**
 * 触发属性事件
 * @param {} sEventName
 */
Control.prototype.triggerAttributeEvent = function(sAttrEventName, a, b, c, d, e) {
	if(this.getAttribute()[sAttrEventName]) {
        if (typeof(this.getAttribute()[sAttrEventName]) == "function") {
            var func = this.getAttribute()[sAttrEventName];
            return func.call(this, this, a, b, c, d, e);
        } else {
            return this.triggerEvent(this.getAttribute()[sAttrEventName],
                a, b, c, d, e);
        }
	}
}

/**
 * 触发事件
 * @param {} sEventName 事件内容 {xx,xx}
 * @param {} a
 * @param {} b
 * @param {} c
 * @param {} d
 * @param {} e
 * @return {}
 */
Control.prototype.triggerEvent = function(sEventName, a, b, c, d, e) {
	var sEvent = this.getScriptValue(sEventName);
	if (sEvent) {
		return this.bindEvent(sEvent, a, b, c, d, e);
	}
}

Control.prototype.setParent = function(oParent) {
	this._parent = oParent;
}

Control.prototype.getParent = function() {
	return this._parent;
}

/**
 * 设置控件所属组件
 * @param {} oComponent
 */
Control.prototype.setComponent = function(oComponent) {
	this._component = oComponent;
}

Control.prototype.getComponent = function() {
	return this._component;
}

Control.prototype.setRoot = function(oRoot) {
	this._root = oRoot;
}

Control.prototype.getRoot = function() {
	return this._root;
}

Control.prototype.setAttribute = function(oAttr) {
	this._attr = oAttr;
}

Control.prototype.getAttribute = function() {
	return this._attr;
}

/**
 * 是否拥有属性
 * @param {} type
 */
Control.prototype.hasAttribute = function(type) {
	var oAttr = this.getAttribute();
	for (var i in oAttr) {
		if (i == type) {
			return true;
		}
	}
	return false;
}

/**
 * 获得html数据
 */
Control.prototype.getHTML = function(){
	return this._html;
}

/**
 * 获得id
 */
Control.prototype.getID = function(){
	if (this.getAttribute()) {
		return this.getAttribute().id;
	}
	return "";
}

/**
 * 获得type
 */
Control.prototype.getType = function(){
	return this._type;
}

/**
 * 设置type
 */
Control.prototype.setType = function(sType){
	this._type = sType;
}

/**
 * 获得数据单元
 */
Control.prototype.getDataUnit = function() {
	return this._dataUnit;
}

/**
 * 设置值
 */
Control.prototype.setValue = function() {
}

/**
 * 获得值
 */
Control.prototype.getValue = function(){
}

/**
 * 获得属性值, 会提交给后台
 */
Control.prototype.getDataAttribute = function(sName) {
	if (sName) {
		return this.getDataUnit().getAttr(sName);
	}
	return this.getDataUnit().getAttribute();
}

/**
 * 设置属性值
 * @param {} oAttr
 */
Control.prototype.setDataAttribute = function(oAttr) {
	this.getDataUnit().setAttribute(oAttr);
}

/**
 * 
 * @param {} name
 * @param {} value
 */
Control.prototype.addDataAttribute = function(sName, sValue) {
	this.getDataUnit().addAttr(sName, sValue);
}

Control.prototype.delDataAttribute = function(sName) {
	this.getDataUnit().delAttr(sName);
}

/**
 * @brief 隐藏控件
 */
Control.prototype.hide = function(){
	$(this.getHTML()).hide();
}

/**
 * @brief 显示控件
 */
Control.prototype.show = function(){
	$(this.getHTML()).show();
}

Control.prototype.initLoad = function(){
}

/**
 * 设置为可用
 */
Control.prototype.setEnable = function(){
	this._disable = false;
}

/**
 * 设置为不可用
 */
Control.prototype.setDisable = function() {
	this._disable = true;
}

/**
 * 是否不可编辑
 */
Control.prototype.isDisable = function() {
	return this._disable;
}


/**
 * 设置为只读
 */
Control.prototype.setReadonly = function() {
	this._readonly = true;
}

/**
 * 取消只读
 */
Control.prototype.setDisReadonly = function() {
	this._readonly = false;
}

/**
 * 是否只读
 */
Control.prototype.isReadonly = function() {
	return this._readonly;
}

Control.prototype.validate = function() {
	this._validate = true;
	this.triggerAttributeEvent("validate");
}

/**
 * 获得是否认证通过
 */
Control.prototype.isValidate = function(){
	return this._validate;
}

/**
 * 设置认证是否通过
 */
Control.prototype.setValidate = function(bValidate, sError){
	this._validate = bValidate;
	if (sError) {
		this.setError(sError);
	}
}

Control.prototype.isContainer = function() {
    return this._container;
}

/**
 * bIDboolean类型如果为true就自动生成id
 */
Control.prototype.clone = function(bId) {
	if(bId){
		this.getAttribute().id =  Math.uuid();
	}
	var sType = this.getType();
	var oAT = this.getAttribute();
	var oControl = eval("new "+ sType +"()");
    oControl.setAttribute(oAT);
	oControl.setElement($(this._elementclone).clone()[0]);
    oControl.setDataSource(this.getDataSource());
	oControl.setParent(this.getParent());
	oControl.setRoot(this.getRoot());
	oControl.setType(sType);
	oControl.setComponent(this.getComponent());
	oControl.init();
    if (oControl.isContainer()) {
        $(oControl.getHTML()).append($(oControl.getElement()).children());
    }
	oControl.initLoad();
	//IE8需要重新绑定事件
    if (!$.support.leadingWhitespace) {
    	oControl.rebuildEvent();
    }
	return oControl;
}

/**
 * 具有path属性的控件可调用此方法， 返回具有dataSource的对象。即表单对象
 * @return {}
 */
Control.prototype.getForm = function() {
	return this._form;
}

Control.prototype.bulidDataSource = function(sValue) {
    var oDataSource = new DataService();
    oDataSource.initLoad();
    oDataSource.setURL(sValue);
    oDataSource.setComponent(this.getComponent());
    oDataSource.setControl(this);
    this.setDataSource(oDataSource);
}

Control.prototype.getDataSource = function() {
    return this._oDS;
}

Control.prototype.setDataSource = function(oDS) {
    this._oDS = oDS;
}

/**
 * 获得数据集
 * @return {}
 */
Control.prototype.getData = function() {
    if (this.getDataSource()) {
        return this.getDataSource().getData();
    }
    return null;
}

/**
 * 设置数据集, 用于静态处理
 * @param {} oData 数组对象
 */
Control.prototype.setData = function(oData) {
    if (this.getDataSource()) {
        this.getDataSource().setData(oData);
    }
}

/**
 * 加载脚本文件
 * @param src
 * @param callback
 */
Control.prototype.loadScriptFile = function(src, callback) {
	$.ajax({
		async: false,
		cache: true,
		type: "GET",
		url: src,
		dataType: "script",
		success: function() {
			if (callback) callback();
		}
	});
}

/**
 * 动态加载css文件公用方法
 */
Control.prototype.loadCSSFile = function(src, addtop){
	if (!addtop && Control._loadCSS[src]) return;
	var fileref = document.createElement('link');
	fileref.setAttribute("rel","stylesheet");
	fileref.setAttribute("type","text/css");
	fileref.setAttribute("href",src);
	document.getElementsByTagName("head")[0].appendChild(fileref);
	if (addtop) {
		window.top.document.getElementsByTagName("head")[0].appendChild(fileref);
	}
	Control._loadCSS[src] = true;
}

/**
 * 移除CSS文件
 * @param src
 */
Control.prototype.unloadCSSFile = function(src) {
	var links = document.getElementsByTagName("link");
	for (var i = 0 ; i < links.length ; i++) {
		if (links[i].getAttribute("href") == src) {
			links[i].parentNode.removeChild(links[i]); 
			delete Control._loadCSS[src];
		}
	}
}


/**
 * 动态delete css文件公用方法
 * @param {} src
 */
Control.prototype.removeCSSFile = function(src) {
	if (src.substring(0, 1) == ".") src = src.substring(1);
	var links = $("link",document.getElementsByTagName("head")[0]);
	for (var i = 0 ; i < links.length ; i++) {
		var link = links[i];
		if (link.href.indexOf(src) != -1) {
			$(link).remove();
		}
	}
}

/**
 * 新建一个控件
 * @param {} sType 控件类型
 * @param {} oAttr 控件属性
 * @return {}
 */
Control.newInstance = function(sType, oAttr) {
	//解决Image图片对象与js原生对象冲突问题
	if (sType == "Image") {
		sType = "Image1";
	}
	//暂时解决加载视屏空间会调用多次视频插件初始化方法
	if (sType == "Device") {
		var oControl = eval("new "+ sType +"()");
		oControl.setAttribute(oAttr);
		oControl.setType(sType);
		return oControl;
	}
	if (!oAttr.id) oAttr.id = Math.uuid();
	var oControl = eval("new "+ sType +"()");
	oControl.setAttribute(oAttr);
	oControl.setType(sType);
	return oControl;
}